<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<%@include file="common.jsp" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改用户信息</title>
</head>
<body>
<form id="userInfoForm" action="${pageContext.request.contextPath}/user/updateUserInfo" method = "post">
		<input type="hidden" name="id" value="${userInfo.id }">
		<br/>
		<br/>
		<input type="text"  name="nickName" value="${userInfo.nickName }">
		<br/>
		<br/>
		<input type="text" name="birthDay" value="<fmt:formatDate pattern="yyyy-MM-dd" value="${userInfo.birthDay}" />">
		<br/>
		<br/>
		<select name="gender" value="${userInfo.gender }" <c:if test="${userInfo.id!=null }">disabled</c:if>>
			<option value="男" <c:if test="${userInfo.gender=='男' }">selected</c:if>>男</option>
			<option value="女" <c:if test="${userInfo.gender=='女' }">selected</c:if>>女</option>
		</select>
		<br/>
		<input type="hidden"  name="cityCode" id="cityCode" value="${userInfo.cityCode }">
		<br/>
		<select id="province" <c:if test="${userInfo.id!=null }">disabled</c:if>>
		</select>
		<br/>
		<br/>
		<select id="city" <c:if test="${userInfo.id!=null }">disabled</c:if>>
		</select>
		<br/>
		<br/>
		<textarea rows="3" cols="100" name="introduce">${userInfo.introduce }</textarea>
		<br/>
		<br/>
		<textarea rows="3" cols="100" name="hope">${userInfo.hope }</textarea>
		<br/>
		<br/>
		<div id="hobbyDiv">
			
		</div>
		<br/>
		<br/>
		<input type="button" id="button" value="保&nbsp;&nbsp;存">
	</form>
	<hr/>
	<br/>
	<br/>
	<c:forEach items="${userInfo.photos }" var="item">
		${item.name }<br/>
	</c:forEach>
	<hr/>
	<br/>
	<br/>
	<c:forEach items="${userInfo.moods }" var="item">
		${item.mood }<br/>
	</c:forEach>
</form>
<hr/>
<br/>
<c:if test="${userInfo.id!=null }">
	<form action="${pageContext.request.contextPath}/user/updateUserContact" method = "post">
			<input type="hidden" name="id" value="${userInfo.id }">
			<br/>
			<br/>
			<input type="text" name="wx" value="${userInfo.wx }">
			<br/>
			<br/>
			<input type="text" name="qq" value="${userInfo.qq }">
			<br/>
			<br/>
			<input type="text" name="tel" value="${userInfo.tel }">
			<br/>
			<br/>
			<input type="text" name="email" value="${userInfo.email }">
			<br/>
			<br/>
			<input type="submit"  value="更新联系方式">
	</form>
</c:if>
<script type="text/javascript">
$(document).ready(function(){
	$.ajax({
        type: 'POST',
        url: '<%=contexPath%>/city/0',
        data: {},
        success: function (data) {
        		$("#province").empty();
         		$.each(data,function(index,item){
         			if(item.code == "${userInfo.cityCode}".substr(0,4)){
         				$("#province").append("<option value="+item.code+" selected>"+item.name+"</option>");
         			} else {
         				$("#province").append("<option value="+item.code+">"+item.name+"</option>");
         			}
         		});
         		citySelect();
        },
        dataType: 'json'
    });
	
    $("#province").change(function(){
    	citySelect();
    });
    $("#city").change(function(){
    	var selectedItem = $("#city").children('option:selected').val();
    });
    
    function citySelect(){
    	var selectedItem = $("#province").children('option:selected').val();
    	$.ajax({
            type: 'POST',
            url: '<%=contexPath%>/city/'+selectedItem,
            data: {},
            success: function (data) {
            		$("#city").empty();
             		$.each(data,function(index,item){
             			if(item.code == "${userInfo.cityCode}"){
             				$("#city").append("<option value="+item.code+" selected>"+item.name+"</option>");
             			} else {
             				$("#city").append("<option value="+item.code+">"+item.name+"</option>");
             			}
             			
             		});
            },
            dataType: 'json'
        });
    }
    
    $("#button").on("click",function(){
    	var selectedItem = $("#city").children('option:selected').val();
    	$("#cityCode").val(selectedItem);
    	$("#userInfoForm").submit();
    });
    
    
    
    $.ajax({
        type: 'POST',
        url: '<%=contexPath%>/hobby/0',
        data: {},
        success: function (data) {
         		$.each(data,function(index,item){
         			if(in_array(item.id,"${userInfo.hobbys}")){
         				$("#hobbyDiv").append("<input name=hobbys type=checkbox value="+item.id+" checked>"+item.desc+"<br/>");
         			} else {
         				$("#hobbyDiv").append("<input name=hobbys type=checkbox value="+item.id+">"+item.desc+"<br/>");
         			}
         			
         		});
        },
        dataType: 'json'
    });
    
    function in_array(search,array){
        for(var i in array){
            if(array[i]==search){
                return true;
            }
        }
        return false;
    }
});
</script>
</body>
</html>